/*
 * @Author: szx
 * @Date: 2021-12-03 12:56:53
 * @LastEditTime: 2021-12-03 13:06:19
 * @Description:
 * @FilePath: \leetcode\700-799\704\704.js
 */

//左闭右闭
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// var search = function (nums, target) {
//     const n = nums.length;
//     let l = 0,
//         r = n - 1;
//     while (l <= r) {
//         const mid = Math.floor((l + r) / 2);
//         if (nums[mid] > target) {
//             r = mid - 1;
//         } else if (nums[mid] == target) {
//             return mid;
//         } else {
//             l = mid + 1;
//         }
//     }
//     return -1;
// };

// 左闭右开
var search = function (nums, target) {
    const n = nums.length;
    let l = 0,
        r = n;
    while (l < r) {
        const mid = Math.floor((l + r) / 2);
        if (nums[mid] > target) {
            r = mid;
        } else if (nums[mid] == target) {
            return mid;
        } else {
            l = mid + 1;
        }
    }
    return -1;
};

console.log(search([-1, 0, 3, 5, 9, 12], 9));
